package main

import (
	"context"
	"log"
	"time"

	"xagent/internal/actors"
	"xagent/internal/distributed"
	"xagent/internal/knowledge"
	"xagent/internal/team"
	proto "xagent/proto/v1"

	"github.com/asynkron/protoactor-go/actor"
)

func main() {
	// 创建 actor system
	system := actor.NewActorSystem()

	// 创建知识库引擎
	rag := knowledge.NewRAGEngine(&knowledge.Config{
		EmbeddingModel: "text-embedding-ada-002",
		VectorStore:    "inmemory",
	})

	// 创建两个团队节点的配置
	node1Config := &distributed.Config{
		NodeName: "team1",
		Address:  "localhost",
		Port:     8090,
		Node: &proto.Node{
			Id:      "node1",
			Address: "localhost:8090",
			Role:    "team1",
			Status:  proto.NodeStatus_NODE_STATUS_ACTIVE,
		},
	}

	node2Config := &distributed.Config{
		NodeName: "team2",
		Address:  "localhost",
		Port:     8091,
		Node: &proto.Node{
			Id:      "node2",
			Address: "localhost:8091",
			Role:    "team2",
			Status:  proto.NodeStatus_NODE_STATUS_ACTIVE,
		},
	}

	// 创建集群管理器
	cluster1, err := distributed.NewClusterManager(system, node1Config)
	if err != nil {
		log.Fatalf("Failed to create cluster1: %v", err)
	}

	cluster2, err := distributed.NewClusterManager(system, node2Config)
	if err != nil {
		log.Fatalf("Failed to create cluster2: %v", err)
	}

	// 创建两个团队
	team1 := team.NewTeam("frontend-team", system, rag)
	team2 := team.NewTeam("backend-team", system, rag)

	// 为前端团队创建角色
	frontendArchitect := actors.NewAIAgent("frontend-architect", "Frontend architecture specialist")
	frontendEngineer := actors.NewAIAgent("frontend-engineer", "Frontend development specialist")
	frontendQA := actors.NewAIAgent("frontend-qa", "Frontend quality specialist")

	// 为后端团队创建角色
	backendArchitect := actors.NewAIAgent("backend-architect", "Backend architecture specialist")
	backendEngineer := actors.NewAIAgent("backend-engineer", "Backend development specialist")
	backendQA := actors.NewAIAgent("backend-qa", "Backend quality specialist")

	// 设置知识库
	frontendArchitect.SetKnowledgeBase(rag)
	frontendEngineer.SetKnowledgeBase(rag)
	frontendQA.SetKnowledgeBase(rag)
	backendArchitect.SetKnowledgeBase(rag)
	backendEngineer.SetKnowledgeBase(rag)
	backendQA.SetKnowledgeBase(rag)

	// 将角色添加到各自的团队
	team1.Hire(frontendArchitect)
	team1.Hire(frontendEngineer)
	team1.Hire(frontendQA)

	team2.Hire(backendArchitect)
	team2.Hire(backendEngineer)
	team2.Hire(backendQA)

	// 启动集群
	ctx := context.Background()
	if err := cluster1.Start(ctx); err != nil {
		log.Fatalf("Failed to start cluster1: %v", err)
	}
	if err := cluster2.Start(ctx); err != nil {
		log.Fatalf("Failed to start cluster2: %v", err)
	}

	// 创建协作会话
	session := &proto.CollaborationSession{
		Id:           "session-1",
		Topic:        "API Design Review",
		Participants: []string{"frontend-architect", "backend-architect"},
		StartTime:    time.Now().UnixNano(),
		Status:       "active",
	}

	// 发起协作
	msg := &proto.AgentMessage{
		Id:   "msg-1",
		Type: proto.MessageType_MESSAGE_TYPE_COLLABORATION,
		Content: &proto.AgentMessage_Message{
			Message: "Review API design for frontend-backend integration",
		},
		Metadata: map[string]string{
			"session_id": session.Id,
			"type":       "api_review",
		},
	}

	// 广播消息给所有参与者
	cluster1.BroadcastMessage(msg)

	// 等待协作完成
	time.Sleep(5 * time.Second)

	// 创建知识共享
	knowledge := &proto.KnowledgeShare{
		Id:      "knowledge-1",
		Content: "API Design Best Practices for Frontend-Backend Integration",
		Tags:    []string{"api", "design", "integration"},
	}

	// 分享知识给所有团队成员
	msg = &proto.AgentMessage{
		Id:   "msg-2",
		Type: proto.MessageType_MESSAGE_TYPE_KNOWLEDGE_SHARE,
		Content: &proto.AgentMessage_Message{
			Message: knowledge.Content,
		},
		Metadata: map[string]string{
			"knowledge_id": knowledge.Id,
			"type":         "best_practices",
		},
	}

	cluster1.BroadcastMessage(msg)
	cluster2.BroadcastMessage(msg)

	// 等待知识共享完成
	time.Sleep(2 * time.Second)

	// 创建团队同步会议
	sync := &proto.TeamSync{
		Id:           "sync-1",
		Type:         "weekly",
		Participants: []string{"frontend-team", "backend-team"},
	}

	msg = &proto.AgentMessage{
		Id:   "msg-3",
		Type: proto.MessageType_MESSAGE_TYPE_TEAM_SYNC,
		Content: &proto.AgentMessage_Message{
			Message: "Weekly Frontend-Backend Sync Meeting",
		},
		Metadata: map[string]string{
			"sync_id": sync.Id,
			"type":    "weekly_sync",
		},
	}

	cluster1.BroadcastMessage(msg)
	cluster2.BroadcastMessage(msg)

	// 等待同步会议完成
	time.Sleep(2 * time.Second)

	// 获取团队指标
	team1Metrics := team1.GetTeamMetrics()
	team2Metrics := team2.GetTeamMetrics()

	log.Printf("Frontend Team Metrics: %+v", team1Metrics)
	log.Printf("Backend Team Metrics: %+v", team2Metrics)

	// 停止集群
	cluster1.Stop()
	cluster2.Stop()

	// 清理资源
	system.Shutdown()
}
